<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #########################################################################################
    # Programmer : nawattapon yoskamtorn						    #
    # Email	: contact@nawattapon.com						    #
    # Website	: http://www.nawattapon.com						    #
    #########################################################################################
    
    class User extends CI_Controller
    {
	function __construct()
	{
	    parent::__construct();
	    if( ! $this->session->userdata('lang')){
                $this->session->set_userdata('lang','english');
            }
            
            if( ! $this->session->userdata('admin_login')){
		redirect('/admin/login/' , 'refresh');
	    }
            
	    $this->template->set_template('backend');
            $this->lang->load('admin/main', $this->session->userdata('lang'));
	    
	    /* check privilege */
	    $action = $this->uri->segment(3) ? $this->uri->segment(3) : 'view';
	    if( ! check_privilege($this->session->userdata['admin']['user_group_id'] , 'video_articles' , $action)){
		show_error( $this->lang->line('permission_denied') );
	    }
            
            $this->load->model('user_model','user_model');
	}
	
	public function index()
	{
	    $data['user_list'] = $this->user_model->lists();
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','user_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','user_view',$data);
            $this->template->render();
	}
	
	public function add()
	{
	    $this->load->model('user_group_model','user_group_model');
	    $where['status'] = '1';
	    $this->user_group_model->set_where($where);
	    $this->user_group_model->set_order_by('title');
	    $data['group_list'] = $this->user_group_model->load_records();
	    
            $this->load->library('form_validation');
	    $this->template->write('title', $this->lang->line('back-end') ,FALSE);
	    $this->template->write_view('header','user_add_js');
	    $this->template->write_view('sidebar','master/sidebar_view');
	    $this->template->write_view('content','user_add_view',$data);
            $this->template->render();
	}
	
	public function add_process()
	{
	    $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'lang:username', 'trim|xss_clean|required|is_unique[users.username]');
            $this->form_validation->set_rules('firstname', 'lang:firstname', 'trim|xss_clean|required');
            $this->form_validation->set_rules('lastname', 'lang:lastname', 'trim|xss_clean|required');
            $this->form_validation->set_rules('email', 'lang:email', 'trim|xss_clean|required|valid_email|is_unique[users.email]');
            $this->form_validation->set_rules('tel', 'lang:contact number', 'trim|xss_clean|required');
            $this->form_validation->set_rules('password', 'lang:password', 'trim|required');
            $this->form_validation->set_rules('c_password', 'lang:confirm password', 'trim|required|matches[password]');
            
            if ($this->form_validation->run() == FALSE)
            {
                $this->add();
            }
            else
            {
		$admin_info = $this->session->userdata('admin');
                $this->load->helper('devlism');
                $input['username'] = $this->input->post('username');
                $input['firstname'] = $this->input->post('firstname');
                $input['lastname'] = $this->input->post('lastname');
                $input['email'] = $this->input->post('email');
                $input['pwd'] = get_encryption($this->input->post('password'));
                $input['contactnumber'] = $this->input->post('tel');
                $input['user_group_id'] = $this->input->post('user_group');
                $input['create_by'] = $admin_info['id'];
                $input['create_date'] = time();
                
                $this->user_model->insert_record($input);
                redirect('/admin/user/' , 'refresh');
            }
	}
    }